﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>WinExist - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The WinExist function checks if the specified window exists and returns the unique ID (HWND) of the first matching window." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>WinExist</h1>
<p>检查指定的窗口是否存在并返回第一个匹配的窗口的唯一 ID(HWND).</p>

<pre class="Syntax">UniqueID := <span class="func">WinExist</span>(<span class="optional">WinTitle, WinText, ExcludeTitle, ExcludeText</span>)</pre>

<h2 id="Parameters">参数</h2>
<dl>

  <dt>WinTitle</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a>, <a href="../Concepts.htm#numbers">整数</a>或<a href="../Objects.htm">对象</a></p>
    <p>识别目标窗口的窗口标题或其他条件. 请参阅 <a href="../misc/WinTitle.htm">WinTitle</a>.</p>
  </dd>

  <dt>WinText</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>如果存在, 此参数必须是目标窗口的单个文本元素的子字符串(和内置的 Window Spy 工具显示的一样). 如果 <a href="DetectHiddenText.htm">DetectHiddenText</a> 为 ON, 那么会检测隐藏文本元素.</p>
  </dd>

  <dt>ExcludeTitle</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>标题中含有此参数值的窗口将被排除.</p>
  </dd>

  <dt>ExcludeText</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>文本中含有此参数值的窗口将被排除.</p>
  </dd>

</dl>

<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
<p>函数返回首个匹配窗口的<a href="../misc/WinTitle.htm#ahk_id">唯一 ID(HWND)</a>(没有找到匹配时则为 0).</p>
<p>因为所有非零数字被视为 "true", 当 <em>WinTitle</em> 存在时, 语句 <code>if WinExist(WinTitle)</code> 为 true.</p>

<h2 id="Remarks">备注</h2>
<p>如果省略所有参数, 将检查<a href="../misc/WinTitle.htm#LastFoundWindow">上次找到的窗口</a>, 看它是否仍然存在.</p>
<p>如果符合条件的窗口存在, 那么<a href="../misc/WinTitle.htm#LastFoundWindow">上次找到的窗口</a>将更新为此窗口.</p>
<p>要发现控件的 HWND(用于 <a href="PostMessage.htm">PostMessage</a>, <a href="SendMessage.htm">SendMessage</a> 或 <a href="DllCall.htm">DllCall</a>), 请使用 <a href="ControlGetHwnd.htm">ControlGetHwnd</a> 或 <a href="MouseGetPos.htm">MouseGetPos</a>.</p>
<p><a href="SetWinDelay.htm">SetWinDelay</a> 不适用于此函数.</p>
<p>窗口标题和文本是区分大小写的. 除非 <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> 被打开, 否则不会检测隐藏窗口.</p>

<h2 id="Related">相关</h2>
<p><a href="WinActive.htm">WinActive</a>, <a href="SetTitleMatchMode.htm">SetTitleMatchMode</a>, <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a>, <a href="../misc/WinTitle.htm#LastFoundWindow">Last Found Window</a>, <a href="ProcessExist.htm">ProcessExist</a>, <a href="WinActivate.htm">WinActivate</a>, <a href="WinWaitActive.htm">WinWaitActive</a>, <a href="WinWait.htm">WinWait</a>, <a href="WinWaitClose.htm">WinWaitClose</a>, <a href="_HotIf.htm">#HotIf</a></p>
<h2 id="Examples">示例</h2>
<div class="ex" id="ExLastFound">
<p><a class="ex_number" href="#ExLastFound"></a> 激活记事本或另一个窗口, 这取决于 WinExist 函数找到的是哪个窗口. 注意, "ahk_" 关键字和它的标准值之间的空格可以省略; 这在使用变量时特别有用, 如第二个 WinExist 所示.</p>
<pre>if WinExist("ahk_class Notepad") or WinExist("ahk_class" ClassName)
    WinActivate <em>; 使用由 WinExist 找到的窗口.</em></pre>
</div>

<div class="ex" id="ExA">
<p><a class="ex_number" href="#ExA"></a> 获取和报告活动窗口的唯一 ID(HWND).</p>
<pre>MsgBox "The active window's ID is " WinExist("A")</pre>
</div>

<div class="ex" id="ExNot">
<p><a class="ex_number" href="#ExNot"></a> 如果计算器不存在则返回.</p>
<pre>if not WinExist("Calculator")
    return</pre>
</div>

</body>
</html>